package com.cswnode.demo.controller;

import com.cswnode.demo.entity.Page;
import com.cswnode.demo.entity.ProfessionalType;
import com.cswnode.demo.service.ProfessionalTypeService;
import com.cswnode.demo.util.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专业类型(ProfessionalType)表控制层
 *
 * @author makejava
 * @since 2021-03-09 16:17:43
 */
@Api(tags = "专业类型")
@RestController
@RequestMapping("professionalType")
public class ProfessionalTypeController {
    /**
     * 服务对象
     */
    @Resource
    private ProfessionalTypeService professionalTypeService;



    @ApiOperation(value = "添加专业类型")
    @PostMapping("/add")
    public ResponseVo add(@RequestBody ProfessionalType type){
        if (type == null){
            return ResponseVo.error("专业类型不能为空！");
        }
        ProfessionalType insert = professionalTypeService.insert(type);
        if (insert == null){
            return ResponseVo.error("添加失败！");
        }
        return ResponseVo.success("添加成功！");
    }

    @ApiOperation(value = "分页查询专业类型")
    @GetMapping("/query")
    public ResponseVo query(@RequestParam(defaultValue = "1") Integer current,
                            @RequestParam(defaultValue = "10")Integer limit, String professionalTypeName){
        Map<String,Object> result = new HashMap<>();
        Page page = new Page();
        page.setCurrent(current);
        page.setLimit(limit);
        page.setRows(professionalTypeService.queryNum(professionalTypeName));
        List<ProfessionalType> professionalTypes = professionalTypeService.queryAllByLimit(page.getOffset(), page.getLimit(), professionalTypeName);
        result.put("page",page);
        result.put("professionalTypes",professionalTypes);
        return ResponseVo.success(result);
    }

        @ApiOperation(value = "查询专业类型")
        @GetMapping("/queryAll")
        public ResponseVo queryProfessionalTypes(){
            List<ProfessionalType> professionalTypes = professionalTypeService.query();
            return ResponseVo.success(professionalTypes);
    }

    @ApiOperation(value = "修改专业类型")
    @PostMapping("/update")
    public ResponseVo update(@RequestBody ProfessionalType professionType){
        if (ObjectUtils.isEmpty(professionType)){
            return  ResponseVo.error("专业信息不得为空！");
        }
        ProfessionalType result = professionalTypeService.update(professionType);
        if (result != null){
            return ResponseVo.success("修改成功！");
        }
        return ResponseVo.error("修改失败");
    }

    @ApiOperation(value = "删除专业类型")
    @GetMapping("/delete/{proId}")
    public ResponseVo delete (@PathVariable("proId")String professionalTypeId){
        if (StringUtils.isEmpty(professionalTypeId)){
            return ResponseVo.error("删除失败！");
        }
        boolean result = professionalTypeService.deleteById(professionalTypeId);
        if (!result){
            return ResponseVo.error("删除失败");
        }
        return ResponseVo.success("删除成功");
    }
}